(function () {
    "use strict";
    angular.forEach(['portalApp', 'bigScreen'], function (module) {
        try {
            var app = angular.module(module);
        } catch (e) {
            return;
        }
        app.requires.push('ngMockE2E');

        app.config(function ($logProvider) {
            $logProvider.debugEnabled(true);
        }).run(function ($httpBackend, urlPrefix) {
            function syncGET(url) {
                var result;
                $.ajax({
                    url: url,
                    async: false,
                    dataType: 'json',
                    success: function (data) {
                        result = data;
                    }
                });
                return result;
            }

            $httpBackend.whenGET(/^(app|conf|mock\-data)\//).passThrough();
            $httpBackend.whenJSONP(/^http:\//).passThrough();

            var onlyMockPanel = (function endsWith(str) {
                return this.slice(-str.length) == str
            }).call(location.pathname, '/0/');
            if (onlyMockPanel) { // 如果仅模拟panel的数据，其他的请求应该pass through
                $httpBackend.whenGET(urlPrefix + '/subSystems.do').passThrough();
                $httpBackend.whenGET(/\/menus\.do\?systemId=/).passThrough();
                $httpBackend.whenGET(/\/menusSearch\.do\?keyword=/).passThrough();
                $httpBackend.whenGET(/\/loginInfo\.do/).passThrough();
                $httpBackend.whenGET(urlPrefix + '/userShortcuts.do').passThrough();
                $httpBackend.whenGET(/\/api\/quickSearchItems\/search\/query/).passThrough();
            }

            $httpBackend.whenGET(/\/checkOldPasswordValid\.do/).passThrough();
            $httpBackend.whenGET(/\/changePassword\.do/).passThrough();

            $httpBackend.whenGET(urlPrefix + '/subSystems.do').respond(function (method, url, data) {
                return [200, syncGET('mock-data/subsystem.json'), {}];
            });
            $httpBackend.whenGET(/\/menus\.do\?systemId=/).respond(function (method, url, data) {
                var systemId = url.match(/systemId=([^&]*)/)[1];
                return [200, syncGET('mock-data/menus-cache.json')[systemId], {}];
            });

            $httpBackend.whenGET(/\/menusSearch\.do\?keyword=/).respond(function (method, url, data) {
                var keyword = decodeURIComponent(url.match(/keyword=([^&]*)/)[1]);
                var menusCache = syncGET('mock-data/menus-cache.json');
                var results = [];
                angular.forEach(menusCache, function (menus) {
                    angular.forEach(menus, function (menu) {
                        if ((menu.MODULE_ID.indexOf(keyword) != -1 || menu.MODULE_NAME.indexOf(keyword) != -1)
                            && menu.MODULE_PATH != null
                            && menu.MODULE_PATH.length > 7) {
                            results.push(menu)
                        }
                    });
                });
                return [200, results, {}];
            });

            $httpBackend.whenGET(/\/loginInfo\.do/).respond(function (method, url, data) {
                return [200, syncGET('mock-data/session.json'), {}];
            });

            $httpBackend.whenGET(urlPrefix + '/userShortcuts.do').respond(function (method, url, data) {
                return [200, syncGET('mock-data/user-shortcuts.json'), {}];
            });

            $httpBackend.whenPOST(/\/addShortcut.do/).passThrough();
            $httpBackend.whenPOST(/\/removeShortcut.do/).passThrough();

            $httpBackend.whenGET(new RegExp("\\/panel\\/settings\\/query\\.do\\?panelId=")).respond(function (method, url) {
                var match = url.match(/query\.do\?panelId=([^\&]+)?/);
                if (match) {
                    return [200, syncGET('mock-data/panel/' + match[1].replace('-', '/') + '.json'), {}];
                } else {
                    return [404, null, {}];
                }
            });

            $httpBackend.whenJSONP(/mock\-data\/.+\.json/).respond(function (method, url) {
                return [200, syncGET(url), {}];
            });

            $httpBackend.whenGET(/\/queryLogicalScreenByPhysicalId\.action\?physicalScreenId=/).respond(function (method, url, data) {
                return [200, syncGET('mock-data/screen/logical-screen.json'), {}];
            });

            $httpBackend.whenGET(/\/loadUnreadNews\.do/).passThrough();
            $httpBackend.whenPOST(/\/recordPopNews\.do/).passThrough();
        });
    });
}());